package com.ws.wsdemo2.test.netty;

import java.util.logging.Logger;

import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 *
 * @author lh
 *
 */
public class TcpClientHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = Logger.getLogger(TcpClientHandler.class.getName());


    /*public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String retUrl = "D:\\workspaces\\eclipse-huifu\\emulator\\xml\\account\\manage\\req.xml";
        String ret = null;
        try {
            ret = FileUtils.readFileToString(new File(retUrl));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ctx.writeAndFlush(ret);
    }

    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        String body = (String)msg;
        System.out.println("server response :\n"+body);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        logger.warning("unexpected exception from downstream:"+ cause.getMessage());
        ctx.close();
    }*/



    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.info("channelActive.");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
        String content = (String) msg;
        //logger.info("############## ctx:" + ctx);
        //logger.info("############## content:" + content);
        /*if (StringUtils.isBlank(content)) {
            logger.error("quote data error :{}", content);
            return;
        }

        // 处理行情
        JSONObject marketData = handleOnePackage(marketServer,content);
        if (marketData != null && checkHandleble(marketServer,marketData.getString("SYMBOL"))) {
            logger.info("############## content:" + content);
            marketProcess.handle(marketServer, marketData);
        }*/
        System.out.println(content);
    }

    // 客户端断开时
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
       /* logger.error("###########与服务器断开连接服务器");
        super.channelInactive(ctx);
        // MsgHandle.getInstance().channel = null;

        // 重新连接服务器
        ctx.channel().eventLoop().schedule(new Runnable() {
            @Override
            public void run() {
                throw new SystemException("Disconnection.....");
            }
        }, 2, TimeUnit.SECONDS);
        ctx.close();*/
    }

    // 超时操作时处理
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
       /* logger.error("#########操作超时");
        if (evt instanceof IdleStateEvent) { // 2
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.READER_IDLE) {
                logger.error(ctx.channel().remoteAddress() + "超时类型：" + event.state().name());
                throw new SystemException("Time out.....type:" + event.state().name());
            } else if (event.state() == IdleState.WRITER_IDLE) {
                // /throw new SystemException("Time out.....type:"+type);
            } else if (event.state() == IdleState.ALL_IDLE) {
                logger.error(ctx.channel().remoteAddress() + "超时类型：" + event.state().name());
                throw new SystemException("Time out.....type:" + event.state().name());
            }
            ctx.writeAndFlush(HEARTBEAT_SEQUENCE.duplicate()).addListener(ChannelFutureListener.CLOSE_ON_FAILURE); // 3
        } else {
            super.userEventTriggered(ctx, evt);
        }*/
    }


}